﻿using System;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Components.Share;
using ASPNETVN.PORTAL.Components.Share.Controls;
using ASPNETVN.PORTAL.Components.UI;

namespace ASPNETVN.PORTAL.Modules.INTRODUCTION.Controls
{
    public partial class Item : ModulePage
    {
        private void BuildPageLink()
        {
            if (Request.QueryString["categoryid"] != null)
            {
                int categoryID = Convert.ToInt32(Request.QueryString["categoryid"]);
                PageLink pagelink = (PageLink)this.ParentControl.FindControl("MainPageLink");
                Category category = Category.Load(categoryID);
                if (category != null)
                {
                    Category parentCategory = Category.Load(category.ParentID);
                    if (parentCategory != null)
                    {
                        pagelink.AddLink(parentCategory.Title, ResolveClientUrl(HttpUrlBuilder.BuildModulePageUrl(this.ModuleID, 0, ModulePages.Item, "categoryid={0}", parentCategory.ID)));

                        string url = ResolveClientUrl(HttpUrlBuilder.BuildModulePageUrl(this.ModuleID, 0, ModulePages.Item, "categoryid={0}", category.ID));
                        pagelink.AddLink(category.Title, url);
                    }
                    else
                    {
                        pagelink.AddLink(category.Title, ResolveClientUrl(HttpUrlBuilder.BuildModulePageUrl(this.ModuleID, 0, ModulePages.Item, "categoryid={0}", category.ID)));
                    }
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                this.BuildPageLink();

                if (Request.QueryString["categoryid"] != null)
                {
                    rptItem.DataSource = Components.Item.SelectCollectionBy_CategoryID(Convert.ToInt32(Request.QueryString["categoryid"]));
                    
                }
                else
                {
                    rptItem.DataSource = Components.Item.GetTopItems(this.ModuleID);
                }


                rptItem.DataBind();
            }
        }
    }
}